#include <sys_config.h>
#include <sys_config.h>
#include <types.h>
#include <basic_types.h>
#include <mediatypes.h>
#include <api/libc/printf.h>
#include <api/libc/string.h>
#include <osal/osal.h>
#ifndef NEW_DEMO_FRAME
#include <api/libpub29/lib_pub27.h>
#include <api/libpub29/lib_as.h>
#endif
#include <hld/decv/decv.h>

#include <api/libosd/osd_lib.h>

#include "osdobjs_def.h"
#include "string.id"
#include "images.id"
#include "osd_config.h"

#include "menus_root.h"
#include "win_com.h"
#include "win_com_popup.h"
#include "win_com_list.h"
#include "win_signalstatus.h"
#ifdef RAM_TMS_TEST
#include "disk_manager.h"
#endif

#if defined(_S3281_) && defined(DVBC_SUPPORT)
#include <hld/nim/nim_tuner.h>
#endif

#ifdef SUPPORT_CAS_A
#include "win_com_menu_define.h"
#endif

/*******************************************************************************
* Objects declaration
*******************************************************************************/
extern CONTAINER g_win_misc_setting;

extern CONTAINER miscset_item0;
extern CONTAINER miscset_item1;
extern CONTAINER miscset_item2;
extern CONTAINER miscset_item3;
extern CONTAINER miscset_item4;
extern CONTAINER miscset_item5;
extern CONTAINER miscset_item6;
extern CONTAINER miscset_item7;
extern CONTAINER miscset_item8;

extern TEXT_FIELD miscset_txt0;
extern TEXT_FIELD miscset_txt1; 
extern TEXT_FIELD miscset_txt2; 
extern TEXT_FIELD miscset_txt3;
extern TEXT_FIELD miscset_txt4;
extern TEXT_FIELD miscset_txt5;
extern TEXT_FIELD miscset_txt6;
extern TEXT_FIELD miscset_txt7;
extern TEXT_FIELD miscset_txt8;

extern TEXT_FIELD miscset_line0;
extern TEXT_FIELD miscset_line1; 
extern TEXT_FIELD miscset_line2; 
extern TEXT_FIELD miscset_line3;
extern TEXT_FIELD miscset_line4;
extern TEXT_FIELD miscset_line5;
extern TEXT_FIELD miscset_line6;
extern TEXT_FIELD miscset_line7;
extern TEXT_FIELD miscset_line8;

extern MULTISEL  miscset_sel0;
extern MULTISEL  miscset_sel1;
extern MULTISEL  miscset_sel2;
extern MULTISEL  miscset_sel3;
extern MULTISEL  miscset_sel4;
extern MULTISEL  miscset_sel5;
extern MULTISEL  miscset_sel6;
extern MULTISEL  miscset_sel7;
extern MULTISEL  miscset_sel8;

static VACTION miscset_item_sel_keymap(POBJECT_HEAD pObj, UINT32 key);
static PRESULT miscset_item_sel_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2);

static VACTION miscset_item_keymap(POBJECT_HEAD pObj, UINT32 key);
static PRESULT miscset_item_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2);

static VACTION miscset_keymap(POBJECT_HEAD pObj, UINT32 key);
static PRESULT miscset_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2);


#define WIN_SH_IDX	  WSTL_WIN_BODYRIGHT_01_HD

#define CON_SH_IDX   WSTL_BUTTON_01_HD
#define CON_HL_IDX   WSTL_BUTTON_05_HD
#define CON_SL_IDX   WSTL_BUTTON_01_HD
#define CON_GRY_IDX  WSTL_BUTTON_07_HD

#define TXT_SH_IDX   WSTL_BUTTON_01_FG_HD
#define TXT_HL_IDX   WSTL_BUTTON_02_FG_HD
#define TXT_SL_IDX   WSTL_BUTTON_01_FG_HD
#define TXT_GRY_IDX  WSTL_BUTTON_07_HD

#define SEL_SH_IDX   WSTL_BUTTON_01_FG_HD
#define SEL_HL_IDX   WSTL_BUTTON_04_HD
#define SEL_SL_IDX   WSTL_BUTTON_01_FG_HD
#define SEL_GRY_IDX  WSTL_BUTTON_07_HD

#ifndef SD_UI
#if Maker_GUI_ON
#ifndef SUPPORT_CAS_A
/* define in win_com_menu_define.h already */
#define	W_L     	Maker_GUI_SM_L
#define	W_T     	Maker_GUI_SM_T
#define	W_W     	Maker_GUI_SM_W
#define	W_H     	Maker_GUI_SM_H
#endif
#else
#ifndef SUPPORT_CAS_A
/* define in win_com_menu_define.h already */
#define	W_L     	248
#define	W_T     	98
#define	W_W     	692
#define	W_H     	488
#endif
#endif

#define CON_L		(W_L+20)
#define CON_T		(W_T + 4)
#if Maker_GUI_ON
#define CON_W		W_W-Maker_GUI_SM_L-(12+2)
#else
#define CON_W		(W_W - 60)
#endif
#define CON_H		40
#define CON_GAP		12

#define TXT_L_OF  	10
#define TXT_W  		300
#define TXT_H		CON_H
#define TXT_T_OF	((CON_H - TXT_H)/2)

#define SEL_L_OF  	(TXT_L_OF + TXT_W)
#define SEL_W  		(CON_W-320)
#define SEL_H		CON_H
#define SEL_T_OF	((CON_H - SEL_H)/2)

#define LINE_L_OF	0
#define LINE_T_OF  	(CON_H+4)
#define LINE_W		CON_W
#define LINE_H     	4
#else
#define	W_L     	105//384
#define	W_T     	57 //138
#define	W_W     	482
#define	W_H     	370

#define CON_L		(W_L+20)
#define CON_T		(W_T + 4)
#define CON_W		(W_W - 40)
#define CON_H		30
#define CON_GAP		12

#define TXT_L_OF  	10
#define TXT_W  		180
#define TXT_H		CON_H
#define TXT_T_OF	((CON_H - TXT_H)/2)

#define SEL_L_OF  	(TXT_L_OF + TXT_W)
#define SEL_W  		(CON_W-20-TXT_W)
#define SEL_H		CON_H
#define SEL_T_OF	((CON_H - SEL_H)/2)

#define LINE_L_OF	0
#define LINE_T_OF  	(CON_H+4)
#define LINE_W		CON_W
#define LINE_H     	4

#endif
#define LDEF_CON(root, varCon,nxtObj,ID,IDl,IDr,IDu,IDd,l,t,w,h,conobj,focusID)		\
    DEF_CONTAINER(varCon,root,nxtObj,C_ATTR_ACTIVE,0, \
    ID,IDl,IDr,IDu,IDd, l,t,w,h, CON_SH_IDX,CON_HL_IDX,CON_SL_IDX,CON_GRY_IDX,   \
    miscset_item_keymap,miscset_item_callback,  \
    conobj, ID,1)
    
#define LDEF_TXT(root,varTxt,nxtObj,l,t,w,h,resID)		\
    DEF_TEXTFIELD(varTxt,root,nxtObj,C_ATTR_ACTIVE,0, \
    0,0,0,0,0, l,t,w,h, TXT_SH_IDX,TXT_HL_IDX,TXT_SL_IDX,TXT_GRY_IDX,   \
    NULL,NULL,  \
    C_ALIGN_LEFT | C_ALIGN_VCENTER, 0,0,resID,NULL)

#define LDEF_MSEL(root, varNum, nxtObj, ID, l, t, w, h,style,cur,cnt,ptabl)	\
    DEF_MULTISEL(varNum,root,nxtObj,C_ATTR_ACTIVE,0, \
    ID,ID,ID,ID,ID, l,t,w,h, SEL_SH_IDX,SEL_HL_IDX,SEL_SL_IDX,SEL_GRY_IDX,   \
    miscset_item_sel_keymap,miscset_item_sel_callback,  \
    C_ALIGN_CENTER | C_ALIGN_VCENTER, 20,0,style,ptabl,cur,cnt)
    

#define LDEF_MENU_ITEM(root,varCon,nxtObj,varTxt,varNum,varLine,ID,IDu,IDd,l,t,w,h,\
						resID,style,cur,cnt,ptbl) \
    LDEF_CON(&root,varCon,nxtObj,ID,ID,ID,IDu,IDd,l,t,w,h,&varTxt,ID)   \
    LDEF_TXT(&varCon,varTxt,&varNum,l + TXT_L_OF,t + TXT_T_OF,TXT_W,TXT_H,resID)    \
    LDEF_MSEL(&varCon,varNum,NULL/*&varLine*/   ,ID, l + SEL_L_OF ,t + SEL_T_OF,SEL_W,SEL_H,style,cur,cnt,ptbl)\
	LDEF_LINE(&varCon,varLine,NULL,1,1,1,1,1,l + LINE_L_OF, t + LINE_T_OF,LINE_W,LINE_H,0,0)

#define LDEF_WIN(varCon,nxtObj,l,t,w,h,focusID)		\
    DEF_CONTAINER(varCon,NULL,NULL,C_ATTR_ACTIVE,0, \
    0,0,0,0,0, l,t,w,h, WIN_SH_IDX,WIN_SH_IDX,WIN_SH_IDX,WIN_SH_IDX,   \
    miscset_keymap,miscset_callback,  \
    nxtObj, focusID,0)

extern UINT16 lnbpower_sw_ids[];
extern UINT16 antenna_power_sw_ids[];	
extern UINT16 chan_sw_ids[];
extern UINT16 chtype_ids[];
extern UINT16 auto_stdy_onoff_ids[];
extern UINT16 ram_tms_onoff_ids[];
extern CHAR*  dvbc_mode_stritem[];
extern CHAR* version_str[];

UINT16 onoff_ids[] = 
{
	RS_COMMON_OFF,
	RS_COMMON_ON,
	RS_SYSTEM_TV_SYSTEM_AUTO
};


LDEF_MENU_ITEM(g_win_misc_setting,miscset_item0,&miscset_item1,miscset_txt0,miscset_sel0,miscset_line0, 1, 9, 2, \
		CON_L, CON_T + (CON_H + CON_GAP)*0, CON_W, CON_H, RS_INFO_LNB_POWER, STRING_ID, 0, 2, lnbpower_sw_ids)


LDEF_MENU_ITEM(g_win_misc_setting,miscset_item1,&miscset_item2,miscset_txt1,miscset_sel1,miscset_line1, 2, 1, 3, \
		CON_L, CON_T + (CON_H + CON_GAP)*1, CON_W, CON_H, 0, STRING_ID, 0, 2, antenna_power_sw_ids)


LDEF_MENU_ITEM(g_win_misc_setting,miscset_item2,&miscset_item3,miscset_txt2,miscset_sel2,miscset_line2, 3, 2, 4, \
		CON_L, CON_T + (CON_H + CON_GAP)*2, CON_W, CON_H, RS_SYSTME_CHANCHANGEG_TYPE, STRING_ID, 0, 3, chan_sw_ids)

LDEF_MENU_ITEM(g_win_misc_setting,miscset_item3,&miscset_item4,miscset_txt3,miscset_sel3,miscset_line3, 4, 3, 5, \
		CON_L, CON_T + (CON_H + CON_GAP)*3, CON_W, CON_H, RS_CHAN_CHANGE, STRING_ID, 0, 2, chtype_ids)

LDEF_MENU_ITEM(g_win_misc_setting,miscset_item4,&miscset_item5,miscset_txt4,miscset_sel4,miscset_line4, 5, 4, 6, \
		CON_L, CON_T + (CON_H + CON_GAP)*4, CON_W, CON_H, RS_BEEPER	     , STRING_ID, 1, 2, onoff_ids)
/* ChengYu@20140522, support user can adjust standby time interval */
LDEF_MENU_ITEM(g_win_misc_setting,miscset_item5,&miscset_item6,miscset_txt5,miscset_sel5,miscset_line5, 6, 5, 7, \
		CON_L, CON_T + (CON_H + CON_GAP)*5, CON_W, CON_H, 0/*RS_BEEPER*/, STRING_ID, 0, 6, auto_stdy_onoff_ids)
		
LDEF_MENU_ITEM(g_win_misc_setting,miscset_item6,&miscset_item7,miscset_txt6,miscset_sel6,miscset_line6, 7, 6, 8, \
		CON_L, CON_T + (CON_H + CON_GAP)*6, CON_W, CON_H, 0, STRING_ID, 0, 2, ram_tms_onoff_ids)
		
LDEF_MENU_ITEM(g_win_misc_setting,miscset_item7,&miscset_item8,miscset_txt7,miscset_sel7,miscset_line7, 8, 7, 9, \
		CON_L, CON_T + (CON_H + CON_GAP)*7, CON_W, CON_H, 0, STRING_ANSI, 0, 2, dvbc_mode_stritem)	

LDEF_MENU_ITEM(g_win_misc_setting,miscset_item8,NULL,miscset_txt8,miscset_sel8,miscset_line8, 9, 8, 1, \
		CON_L, CON_T + (CON_H + CON_GAP)*8, CON_W, CON_H, 0, STRING_ANSI, 0, 1, version_str)


LDEF_WIN(g_win_misc_setting,&miscset_item0,W_L, W_T, W_W, W_H, 1)

typedef enum
{
	LNBPOWER_ID = 1,
	ANTENNAPOWER_ID,		
	CHANCHG_CHAN_ID,
	CHANCHG_VIDEO_ID,
	INSTALLBEEP_ID,
    AUTO_STANDBY_ID,
    RAM_TMS_ID,
    DVBC_MODE_ID,
}MISC_SETTING_ID;
/*******************************************************************************
*	Local functions & variables declare
*******************************************************************************/
UINT16 lnbpower_sw_ids[] = 
{
	RS_COMMON_OFF,
	RS_COMMON_ON
};

UINT16 antenna_power_sw_ids[] =   
{
	RS_COMMON_OFF,
	RS_COMMON_ON
};

UINT16 chan_sw_ids[] = 
{
    RS_INSTALLATION_SCAN_MODE_FREE,
    RS_SYSTME_CHANNEL_SCRAMBLED,
    RS_INSTALLATION_SCAN_MODE_ALL
};

UINT16 chtype_ids[] =
{
	RS_CHAN_CHANGE_BLACKSCREEN,
	RS_CHAN_CHANGE_FREEZEVIDEO,
};

//ChengYu@20140522, support user can adjust standby time interval
UINT16 auto_stdy_onoff_ids[] = 
{
	RS_COMMON_OFF,
	RS_TIMER_1HR,
	RS_TIMER_2HR,
	RS_TIMER_4HR,
	RS_TIMER_8HR,
	RS_TIMER_12HR
};

UINT16 ram_tms_onoff_ids[] = 
{
	RS_COMMON_OFF,
	RS_COMMON_ON
};

CHAR*  dvbc_mode_stritem[] = 
{
	"J83AC",
	"J83B"	
};

CHAR *version_str[] = {" Jan 01 00:00:00 2013  "};

CONTAINER* misc_menu_items[] = 
{
    &miscset_item0,
    &miscset_item1,
    &miscset_item2,
    &miscset_item3,
	&miscset_item4,
	&miscset_item5,
	&miscset_item6,
	&miscset_item7,
};

static void win_miscset_load_setting(void);
static void win_miscset_save_setting(void);
static void check_front_end_is_S(POBJECT_HEAD pObj);

/*******************************************************************************
*	key mapping and event callback definition
*******************************************************************************/
static VACTION miscset_item_sel_keymap(POBJECT_HEAD pObj, UINT32 key)
{
	VACTION act;
	switch(key)
	{
  	case V_KEY_LEFT:
        act = VACT_DECREASE;
        break;
    case V_KEY_RIGHT:
        act = VACT_INCREASE;
		break; 
	default:
		act = VACT_PASS;
	}

	return act;	
}

static PRESULT miscset_item_sel_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2)
{
	PRESULT ret = PROC_PASS;
	UINT32 sel;
	UINT8 bID;
	SYSTEM_DATA* sys_data;

	bID = OSD_GetObjID(pObj);

	sys_data = sys_data_get();

	switch(event)
	{
	case EVN_PRE_CHANGE:
		sel = *((UINT32*)param1);
		break;
	case EVN_POST_CHANGE:
		sel = param1;
		if(bID == LNBPOWER_ID)
		{
			if(sel == 0)
				SetLNBShortDetect(sel);
			sys_data->bLNB_power = sel;
			//api_LNB_power(sys_data->bLNB_power);
            board_lnb_power(0, system_config.bLNB_power);
            board_lnb_power(1, system_config.bLNB_power);
			SetLNBShortDetectStart(sys_data->bLNB_power);
			if(sel == 1)
				SetLNBShortDetect(sel);
		}
		else if(bID == ANTENNAPOWER_ID)
		{
			sys_data->antenna_power = sel;
			board_antenna_power(0, system_config.antenna_power);
			board_antenna_power(1, system_config.antenna_power);
		}
		else if(bID == CHANCHG_CHAN_ID)
			sys_data->chan_sw = sel;
		else if(bID == CHANCHG_VIDEO_ID)
			sys_data->chchgvideo_type = sel;
    	else if(bID == INSTALLBEEP_ID)
			sys_data->ber_printf = sel;
        else if(bID == AUTO_STANDBY_ID)
            sys_data->auto_standby_en= sel;
/*
        else if(bID == RAM_TMS_ID)
            sys_data->ram_tms_en = sel;
*/    
#if defined(_S3281_) && defined(DVBC_SUPPORT)
	else if(bID == DVBC_MODE_ID)
	{
		//libc_printf("sel = %d ,%s\n",sel,__FUNCTION__);
		g_nim_dev = dev_get_by_id(HLD_DEV_TYPE_NIM, 0);  
		struct DEMOD_CONFIG_ADVANCED demod_config;
					 
   		if(NULL != g_nim_dev)
   		{				
			front_end_c_get_QAM_config(sel,NIM_SAMPLE_CLK_27M,&demod_config);
			nim_ioctl_ext(g_nim_dev, NIM_DRIVER_SET_NIM_MODE, (void *)&demod_config);
		}
		
		sys_data->dvbc_mode= sel;
		sys_data_save(1);
		
		UINT8 back_saved;
		win_compopup_init(WIN_POPUP_TYPE_SMSG); 					   
		win_compopup_set_msg_ext("STB will reboot after 1s ... ", NULL, 0);
		win_compopup_open_ext(&back_saved);
		osal_task_sleep(1000);
		win_compopup_smsg_restoreback();

		key_pan_display("OFF ",4);
		osal_task_sleep(500);
		power_off_process(0);
		power_on_process();
		
	}
#endif
        break; 
	}
 	return ret;
}

static VACTION miscset_item_keymap(POBJECT_HEAD pObj, UINT32 key)
{
	VACTION act = VACT_PASS;

	return act;	
}

static PRESULT miscset_item_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2)
{
	PRESULT ret = PROC_PASS;

	return ret;
}

static VACTION miscset_keymap(POBJECT_HEAD pObj, UINT32 key)
{
	VACTION act;
	switch(key)
	{
  	case V_KEY_UP:
        act = VACT_CURSOR_UP;
        break;
    case V_KEY_DOWN:
        act = VACT_CURSOR_DOWN;
		break;
	case V_KEY_EXIT:
	case V_KEY_MENU: 
		act = VACT_CLOSE;
		break;
	default:
		act = VACT_PASS;
	}

	return act;	
}

static PRESULT miscset_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2)
{
	PRESULT ret = PROC_PASS;
	CONTAINER* root = (CONTAINER*) pObj;
	
	switch(event)
	{
	case EVN_PRE_OPEN:
		win_miscset_load_setting();
		check_front_end_is_S(pObj);
		wincom_open_title(pObj,RS_OTHER,0);
		break;
	case EVN_POST_OPEN:
		break;
	case EVN_PRE_CLOSE:
		win_miscset_save_setting();
		/* Make OSD not flickering */
		*((UINT32*)param2) &= ~C_CLOSE_CLRBACK_FLG;	
		break;
	case EVN_POST_CLOSE:
		break;
	case EVN_MSG_GOT:
 		break;
		
	}

	return ret;
}

static void win_miscset_load_setting(void)
{
	CONTAINER	*win,*scditem,*lastitem;
	MULTISEL	*msel;
	TEXT_FIELD *pTxt;
	SYSTEM_DATA* sys_data;
	POBJECT_HEAD pObj;
	BOOL beeper_flag,lnb_power_flag,chchg_video_flag,antenna_power_flag;

	sys_data = sys_data_get();
	/* LNB Power */
	msel =&miscset_sel0;
	OSD_SetMultiselSel(msel, sys_data->bLNB_power);

	msel =&miscset_sel1;
	pTxt = &miscset_txt1;  
	OSD_SetMultiselSel(msel, sys_data->antenna_power); 
	OSD_SetTextFieldStrPoint(pTxt,display_strs[1]);
	OSD_SetTextFieldContent(pTxt,STRING_ANSI,(UINT32)"Antenna Power");
	
	/* Change change channel type */
	if(sys_data->chan_sw>=3)
		sys_data->chan_sw = 0;
	msel =&miscset_sel2;
	OSD_SetMultiselSel(msel, sys_data->chan_sw);

	/* Change change video type */
	msel =&miscset_sel3;
	OSD_SetMultiselSel(msel, sys_data->chchgvideo_type);

	/* Beep */
	msel =&miscset_sel4;
	OSD_SetMultiselSel(msel, sys_data->install_beep);

    
    win = &g_win_misc_setting;    
#if( SYS_LNB_POWER_OFF ==  SYS_FUNC_ON)
	lnb_power_flag = TRUE;
#else
	lnb_power_flag = FALSE;
#endif

#if (defined(DVBT_SUPPORT) || defined(DVBT2_SUPPORT))
    antenna_power_flag = TRUE;
#else
    antenna_power_flag = FALSE;
#endif
#ifdef CHANCHG_VIDEOTYPE_SUPPORT
	chchg_video_flag = TRUE;
#else
	chchg_video_flag = FALSE;
#endif

#ifdef ANTENNA_INSTALL_BEEP
	beeper_flag = TRUE;
#else
	beeper_flag = FALSE;
#endif

	if(!lnb_power_flag)
		remove_menu_item(win, (OBJECT_HEAD *)misc_menu_items[LNBPOWER_ID - 1], CON_H + CON_GAP);
	if(!antenna_power_flag)
	    //remove_menu_item(win, (OBJECT_HEAD *)misc_menu_items[ANTENNAPOWER_ID - 1], CON_H + CON_GAP);//Martin@20150429_ANT_Power
	if(!chchg_video_flag)
		remove_menu_item(win, (OBJECT_HEAD *)misc_menu_items[CHANCHG_VIDEO_ID - 1], CON_H + CON_GAP);  
    if(!beeper_flag)
        remove_menu_item(win, (OBJECT_HEAD *)misc_menu_items[INSTALLBEEP_ID - 1], CON_H + CON_GAP);

#ifndef RAM_TMS_TEST
    remove_menu_item(win, (OBJECT_HEAD *)&miscset_item6, CON_H + CON_GAP);
#endif

#if !defined(AUTOMATIC_STANDBY)
    remove_menu_item(win,(POBJECT_HEAD)&miscset_item4,CON_H + CON_GAP);
#endif

#if ((!defined(_S3281_) ||!defined(DVBC_SUPPORT)) || defined _32M_PVR_)
    remove_menu_item(win,(POBJECT_HEAD)&miscset_item7,CON_H + CON_GAP);		
#endif


    /*Disable/Endable auto standby*/
    msel = &miscset_sel5;
    pTxt = &miscset_txt5;
    OSD_SetMultiselSel(msel,sys_data->auto_standby_en);
    //OSD_SetTextFieldStrPoint(pTxt,display_strs[2]);
    //OSD_SetTextFieldContent(pTxt,STRING_ANSI,(UINT32)"Auto Standby");
    OSD_SetTextFieldContent(pTxt,STRING_ID,RS_SYSTEM_AUTO_STANDBY);

	/* Disable/Endable ram timeshift */
#ifdef RAM_TMS_TEST
    msel = &miscset_sel6;
    pTxt = &miscset_txt6;
    OSD_SetMultiselSel(msel,sys_data->ram_tms_en);
    OSD_SetTextFieldStrPoint(pTxt,display_strs[3]);
    OSD_SetTextFieldContent(pTxt,STRING_ANSI,(UINT32)"RAM Timeshift");
#endif

#if defined(_S3281_) && defined(DVBC_SUPPORT)
    msel = &miscset_sel7;
    OSD_SetMultiselSel(msel, sys_data->dvbc_mode);
    pTxt = &miscset_txt7;    
    OSD_SetTextFieldStrPoint(pTxt,display_strs[4]);
    OSD_SetTextFieldContent(pTxt,STRING_ANSI,(UINT32)"DVBC Mode");
    if(sys_data_get_group_num() > 0)
    {
		OSD_SetAttr(&miscset_item7, C_ATTR_INACTIVE);
		OSD_SetAttr(&miscset_txt7, C_ATTR_INACTIVE);
		OSD_SetAttr(&miscset_sel7, C_ATTR_INACTIVE);
    }
    else
    {
		OSD_SetAttr(&miscset_item7, C_ATTR_ACTIVE);
		OSD_SetAttr(&miscset_txt7, C_ATTR_ACTIVE);
		OSD_SetAttr(&miscset_sel7, C_ATTR_ACTIVE);
    }
#endif

#if defined(SUPPORT_CAS9) | defined(SUPPORT_CAS7) 
	strncpy(version_str[0], get_ca_lib_ver()+4, 21);
    msel = &miscset_sel8;
    pTxt = &miscset_txt8;
    OSD_SetMultiselSel(msel,0);
    OSD_SetTextFieldStrPoint(pTxt,display_strs[5]);
    OSD_SetTextFieldContent(pTxt,STRING_ANSI,(UINT32)"LIB Version");
#else
	remove_menu_item(win, (OBJECT_HEAD *)&miscset_item8, CON_H + CON_GAP);
#endif

}

static void win_miscset_save_setting(void)
{
	MULTISEL	*msel;
	SYSTEM_DATA* sys_data;
	UINT32 val;

	sys_data = sys_data_get();

#ifndef NEW_DEMO_FRAME
	struct nim_lnb_info lnb_info;
	for(i=0;i<2;i++)
	{
        lib_nimg_get_lnb_info(i+1,&lnb_info);
        if(LNB_POWER_OFF == sys_data->bLNB_power)
            lnb_info.lnb_power_off = 1;
        else
            lnb_info.lnb_power_off = 0;                      
		lib_nimg_set_lnb_info(i+1,&lnb_info);
	}
#else
	struct nim_config lnb_info;
	struct nim_device *nim;
    UINT32 sub_type;
    UINT16 i;
	for(i=0;i<2;i++)
	{
        nim = (struct nim_device *)dev_get_by_id(HLD_DEV_TYPE_NIM, i);
        sub_type = dev_get_device_sub_type(nim, HLD_DEV_TYPE_NIM);
        dev_get_nim_config(nim, sub_type, &lnb_info);
        if(LNB_POWER_OFF == sys_data->bLNB_power)
            lnb_info.antenna.antenna_enable = /*0*/1;   //Seiya fix BUG37540:
        else
            lnb_info.antenna.antenna_enable = 0;// 1;   //Evan BUG45490 
            
	    dev_set_nim_config(nim, sub_type, &lnb_info);
	}
#endif

	msel =&miscset_sel1;
	val = OSD_GetMultiselSel(msel);
	sys_data->antenna_power= val;
	
	msel =&miscset_sel2;
	val = OSD_GetMultiselSel(msel);
	sys_data->chan_sw = val;
	
	msel =&miscset_sel3;
	val = OSD_GetMultiselSel(msel);
	sys_data->chchgvideo_type = val;

#ifdef CHANCHG_VIDEOTYPE_SUPPORT
	UIChChgSetVideoType(sys_data->chchgvideo_type);
#endif
	
	msel =&miscset_sel4;
	val = OSD_GetMultiselSel(msel);
	sys_data->install_beep = val;

    msel =&miscset_sel5;
	val = OSD_GetMultiselSel(msel);
	sys_data->auto_standby_en= val;

#ifdef RAM_TMS_TEST
    msel =&miscset_sel6;
	val = OSD_GetMultiselSel(msel);

	char rec_part[16];
	char tms_part[16];

	if (sys_data->ram_tms_en != val) // detach tms part
	{
		rec_part[0] = tms_part[0] = 0;
		pvr_get_cur_mode(rec_part, tms_part);
		if (tms_part[0] != 0)
		{
			if (STRCMP(rec_part, tms_part) == 0)
				pvr_set_disk_use(PVR_REC_ONLY_DISK, rec_part);
			else
				pvr_detach_part(tms_part, PVR_TMS_ONLY_DISK);
			api_pvr_adjust_tms_space();
		}
	}
	
	if (sys_data->ram_tms_en == 0 && val == 1) // enable RAM disk timeshift
	{
		UINT32 ram_len = RAM_DISK_SIZE;	
		UINT32 ram_addr = (void *)(RAM_DISK_ADDR & 0x0fffffff | 0x80000000);

		ramdisk_create((UINT32)ram_addr, ram_len);

		struct pvr_register_info pvr_reg_info;
		MEMSET(&pvr_reg_info, 0, sizeof(struct pvr_register_info));
		STRCPY(pvr_reg_info.mount_name, "/mnt/rda1");
		pvr_reg_info.disk_usage = PVR_TMS_ONLY_DISK;
		pvr_reg_info.sync = 1;
		pvr_reg_info.init_list = 0;
		pvr_reg_info.check_speed = 0;
		pvr_register((UINT32)&pvr_reg_info, 0);
	}
	else if (sys_data->ram_tms_en == 1 && val == 0) // disable RAM disk timeshift
	{
		ramdisk_delete();
	}

	if (sys_data->ram_tms_en != val)
	{
		if (val == 0) // disable RAM disk timeshift, select PVR partition again
		{
			UINT8 back_saved;
			rec_part[0] = tms_part[0] = 0;
			pvr_select_part(rec_part, tms_part);
			if (rec_part[0] != 0 || tms_part[0] != 0)
			{
				win_compopup_init(WIN_POPUP_TYPE_SMSG); 					   
				win_compopup_set_msg_ext("Init PVR partitions, please wait...", NULL, 0);
				win_compopup_open_ext(&back_saved);
				if (STRCMP(rec_part, tms_part) == 0)
				{
					pvr_change_part(rec_part, PVR_REC_AND_TMS_DISK);
				}
				else
				{
					pvr_change_part(rec_part, PVR_REC_ONLY_DISK);
					pvr_change_part(tms_part, PVR_TMS_ONLY_DISK);
				}
				win_compopup_smsg_restoreback();
			}
		}
		
		struct dvr_HDD_info hdd_info;
		pvr_get_HDD_info(&hdd_info);
		api_pvr_check_level(&hdd_info);
		if (pvr_get_cur_mode(NULL, NULL) == PVR_DISK_INVALID)
		{
			api_pvr_clear_up_all();
		}
	}
	sys_data->ram_tms_en = val;
#endif

    sys_data_save(1);
    
}

static void check_front_end_is_S(POBJECT_HEAD pObj)
{
    if((board_get_frontend_type(0)!=FRONTEND_TYPE_S)&&(board_get_frontend_type(1)!=FRONTEND_TYPE_S))
	{
		OSD_SetAttr(&miscset_item0, C_ATTR_INACTIVE);       //LNB_power
		OSD_SetAttr(&miscset_txt0, C_ATTR_INACTIVE);
		OSD_SetAttr(&miscset_sel0, C_ATTR_INACTIVE);
	}

	if((board_get_frontend_type(0)!=FRONTEND_TYPE_T)&&(board_get_frontend_type(1)!=FRONTEND_TYPE_T))
	{
		OSD_SetAttr(&miscset_item1, C_ATTR_INACTIVE);   //antenna_power
		OSD_SetAttr(&miscset_txt1, C_ATTR_INACTIVE);
		OSD_SetAttr(&miscset_sel1, C_ATTR_INACTIVE);
	}	
}
